package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.context.BaseContext;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询
     *
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {

        Page<Dish> pageInfo = new Page<>(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> dishVOPage = new Page<>();

        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //过滤条件
        queryWrapper.like(dishPageQueryDTO.getName() != null, Dish::getName, dishPageQueryDTO.getName())
                .eq(dishPageQueryDTO.getCategoryId() != null, Dish::getCategoryId, dishPageQueryDTO.getCategoryId())
                .eq(dishPageQueryDTO.getStatus() != null, Dish::getStatus, dishPageQueryDTO.getStatus())
                //排序条件
                .orderByDesc(Dish::getPrice);
        //执行分页查询
        this.page(pageInfo, queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo, dishVOPage);

        List<Dish> records = pageInfo.getRecords();

        List<DishVO> list = records.stream().map((item) -> {
            DishVO dishVO = new DishVO();

            BeanUtils.copyProperties(item, dishVO);

            Long categoryId = item.getCategoryId();
            //根据id查询分类对象
            Category category = categoryService.getById(categoryId);

            if (category != null) {
                String dishName = category.getName();
                dishVO.setCategoryName(dishName);
            }
            return dishVO;
        }).collect(Collectors.toList());

        dishVOPage.setRecords(list);

        PageResult pageResult = new PageResult();
        pageResult.setTotal(dishVOPage.getTotal());
        pageResult.setRecords(dishVOPage.getRecords());

        return pageResult;
    }

    /**
     * 新增菜品
     * @param dishVO
     */
    @Override
    public void save(DishVO dishVO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishVO, dish);
        this.save(dish);
    }


    /**
     * 根据id查询单个菜品信息
     */
    @Override
    public Dish getById(Integer id) {
        Dish dish = this.getById(id);
        return dish;
    }

    /**
     * 修改菜品
     */
    @Override
    public void update(DishVO dishVO) {

        Dish dish = new Dish();
        LambdaUpdateWrapper<Dish> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Dish::getId, dishVO.getId());
        BeanUtils.copyProperties(dishVO, dish);

        this.update(dish, updateWrapper);
    }

    /**
     * 批量删除菜品
     */
    @Override
    public void remove(long[] ids) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId, ids);

        this.remove(queryWrapper);
    }

    /**
     * 菜品起售、停售
     */
    @Override
    public void updateStatus(Long status, Long id) {

        LambdaUpdateWrapper<Dish> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Dish::getId, id);
        updateWrapper.set(Dish::getStatus, status);

        this.update(updateWrapper);
    }

    /**
     * 根据分类id查询菜品
     */
    @Override
    public List<DishVO> list(Long categoryId) {

        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId, categoryId);

        List<Dish> list = this.list(queryWrapper);

        List<DishVO> dishVOList = new ArrayList<>();

        BeanUtils.copyProperties(list, dishVOList);

        return dishVOList;
    }


}
